#ifndef WEBSOCKET_CLIENT_H
#define WEBSOCKET_CLIENT_H

#include <string>
#include <thread>
#include <atomic>
#include <functional>

namespace websocket {

class WebSocketClient {
public:
    using MessageHandler = std::function<void(const std::string&)>;

    WebSocketClient(const std::string& host = "127.0.0.1", int port = 8080);
    ~WebSocketClient();
    
    // 连接和断开连接
    bool connect();
    void disconnect();
    
    // 发送消息
    void sendText(const std::string& message);
    
    // 设置消息处理回调
    void setMessageHandler(MessageHandler handler);
    
    bool isConnected() const { return connected_; }

private:
    void readLoop();
    std::string createHandshakeRequest();
    bool sendData(const char* data, size_t length);
    bool readData(char* buffer, size_t size);

    std::string host_;
    int port_;
    int socket_;
    std::atomic<bool> connected_;
    std::thread read_thread_;
    MessageHandler message_handler_;
};

} // namespace websocket

#endif // WEBSOCKET_CLIENT_H